﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Globalization;
public partial class Main_Admin_ViewPurchaseOrderLog : System.Web.UI.Page
{
    DataSet DsPurchaseOrder = new DataSet();
    DataSet DsAppLog = new DataSet();
    DataTable DtLog = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                //GetData();
                AppGridGetData();
            }
        }
        catch (Exception ex)
        {
        }
    }
    protected void AppGridGetData()
    {
        try
        {
            string FolderPath = Server.MapPath("~/XML/");
            string[] fileName = Directory.GetFiles(FolderPath, "Log_*.xml", System.IO.SearchOption.TopDirectoryOnly);
            if (fileName.Length > 0)
            {
                //for delete the File.                
                foreach (string file in fileName)
                {
                    string[] splitfileName = file.Split('_');
                    string[] SpletedData;
                    if (splitfileName[splitfileName.Length - 1].Contains('.'))
                    {
                        SpletedData = splitfileName[splitfileName.Length - 1].Split('.');
                        DateTime FileDate = Convert.ToDateTime(SpletedData[0].ToString());
                        DateTime Today = DateTime.Now; //for today Date
                        DateTime DateDiff = Today - TimeSpan.FromDays(30);//for Calculate the Date difference.
                        if (FileDate < DateDiff)
                        {
                            //File Is Deleted.
                            File.Delete(file);
                        }
                    }
                }
            }
            string Path = Server.MapPath("~/XML/");

            string[] LoadGeneralfileName = Directory.GetFiles(Path, "Log_General_*.xml", System.IO.SearchOption.TopDirectoryOnly);
            string[] LoadItemfileName = Directory.GetFiles(Path, "Log_Item_*.xml", System.IO.SearchOption.TopDirectoryOnly);
            string[] LoadPurchasefileName = Directory.GetFiles(Path, "Log_PurchaseOrder_*.xml", System.IO.SearchOption.TopDirectoryOnly);
            string[] LoadPurchaseReceivefileName = Directory.GetFiles(Path, "Log_PurchaseOrderReceive_*.xml", System.IO.SearchOption.TopDirectoryOnly);
            string[] LoadItemRequestfileName = Directory.GetFiles(Path, "Log_SetItemRequest_*.xml", System.IO.SearchOption.TopDirectoryOnly);
            //For Load the File
            DataTable DtAppLog = new DataTable();
            DtAppLog.Columns.Add("userid", typeof(string));
            DtAppLog.Columns.Add("logtype", typeof(string));
            DtAppLog.Columns.Add("logmesg", typeof(string));
            DtAppLog.Columns.Add("pagename", typeof(string));
            DtAppLog.Columns.Add("ipaddr", typeof(string));
            DtAppLog.Columns.Add("datetime", typeof(string));

            //For the Purchase Order File.
            #region PurchaseOrder
            foreach (string file in LoadPurchasefileName)
            {
                string[] SplitData = file.Split('_');
                string[] Message;
                DsAppLog.ReadXml(file);//Read the XML file and fill it into the Datatable.                    
                if (DsAppLog.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < DsAppLog.Tables[0].Rows.Count; i++)
                    {
                        DataRow DtRow = DtAppLog.NewRow();
                        DtRow["userid"] = DsAppLog.Tables[0].Rows[i]["userid"].ToString();
                        DtRow["logtype"] = DsAppLog.Tables[0].Rows[i]["logtype"].ToString();
                        Message = DsAppLog.Tables[0].Rows[i]["logmesg"].ToString().Split(',');
                        for (int k = 0; k < Message.Length; k++)
                        {
                            DtRow["logmesg"] += Message[k] + "<br/>";
                        }
                        DtRow["pagename"] = DsAppLog.Tables[0].Rows[i]["pagename"].ToString();
                        DtRow["ipaddr"] = DsAppLog.Tables[0].Rows[i]["ipaddr"].ToString();
                        DtRow["datetime"] = DsAppLog.Tables[0].Rows[i]["datetime"].ToString();
                        DtAppLog.Rows.Add(DtRow);
                    }
                    grdviewPurchaseOrder.DataSource = DtAppLog;
                    grdviewPurchaseOrder.DataBind();
                }
                DsAppLog.Tables[0].Clear();
            }
            DtAppLog.Rows.Clear();
            #endregion
            //For the Purchase Receive File.
            #region Purchase Receive
            foreach (string file in LoadPurchaseReceivefileName)
            {
                string[] SplitData = file.Split('_');
                string[] Message;
                DsAppLog.ReadXml(file);//Read the XML file and fill it into the Datatable.      
                if (DsAppLog.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < DsAppLog.Tables[0].Rows.Count; i++)
                    {
                        DataRow DtRow = DtAppLog.NewRow();
                        DtRow["userid"] = DsAppLog.Tables[0].Rows[i]["userid"].ToString();
                        DtRow["logtype"] = DsAppLog.Tables[0].Rows[i]["logtype"].ToString();
                        Message = DsAppLog.Tables[0].Rows[i]["logmesg"].ToString().Split(',');
                        for (int k = 0; k < Message.Length; k++)
                        {
                            DtRow["logmesg"] += Message[k] + "<br/>";
                        }
                        DtRow["pagename"] = DsAppLog.Tables[0].Rows[i]["pagename"].ToString();
                        DtRow["ipaddr"] = DsAppLog.Tables[0].Rows[i]["ipaddr"].ToString();
                        DtRow["datetime"] = DsAppLog.Tables[0].Rows[i]["datetime"].ToString();
                        DtAppLog.Rows.Add(DtRow);
                    }
                    grdviewPurchaseReceive.DataSource = DtAppLog;
                    grdviewPurchaseReceive.DataBind();
                }
                DsAppLog.Tables[0].Clear();
            }
            DtAppLog.Rows.Clear();
            #endregion
            //For the Item Request File.
            #region ItemRequest
            foreach (string file in LoadItemfileName)
            {
                string[] SplitData = file.Split('_');
                string[] Message;
                DsAppLog.ReadXml(file);//Read the XML file and fill it into the Datatable.     
                if (DsAppLog.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < DsAppLog.Tables[0].Rows.Count; i++)
                    {
                        DataRow DtRow = DtAppLog.NewRow();
                        DtRow["userid"] = DsAppLog.Tables[0].Rows[i]["userid"].ToString();
                        DtRow["logtype"] = DsAppLog.Tables[0].Rows[i]["logtype"].ToString();
                        Message = DsAppLog.Tables[0].Rows[i]["logmesg"].ToString().Split(',');
                        for (int k = 0; k < Message.Length; k++)
                        {
                            DtRow["logmesg"] += Message[k] + "<br/>";
                        }
                        DtRow["pagename"] = DsAppLog.Tables[0].Rows[i]["pagename"].ToString();
                        DtRow["ipaddr"] = DsAppLog.Tables[0].Rows[i]["ipaddr"].ToString();
                        DtRow["datetime"] = DsAppLog.Tables[0].Rows[i]["datetime"].ToString();
                        DtAppLog.Rows.Add(DtRow);
                    }
                    grdviewItem.DataSource = DtAppLog;
                    grdviewItem.DataBind();
                }
                DsAppLog.Tables[0].Clear();
            }
            DtAppLog.Rows.Clear();
            #endregion
            //for the Item Master File
            #region ItemMaster
            foreach (string file in LoadItemRequestfileName)
            {
                string[] SplitData = file.Split('_');
                string[] Message;
                DsAppLog.ReadXml(file);//Read the XML file and fill it into the Datatable.     
                if (DsAppLog.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < DsAppLog.Tables[0].Rows.Count; i++)
                    {
                        DataRow DtRow = DtAppLog.NewRow();
                        DtRow["userid"] = DsAppLog.Tables[0].Rows[i]["userid"].ToString();
                        DtRow["logtype"] = DsAppLog.Tables[0].Rows[i]["logtype"].ToString();
                        Message = DsAppLog.Tables[0].Rows[i]["logmesg"].ToString().Split(',');
                        for (int k = 0; k < Message.Length; k++)
                        {
                            DtRow["logmesg"] += Message[k] + "<br/>";
                        }
                        DtRow["pagename"] = DsAppLog.Tables[0].Rows[i]["pagename"].ToString();
                        DtRow["ipaddr"] = DsAppLog.Tables[0].Rows[i]["ipaddr"].ToString();
                        DtRow["datetime"] = DsAppLog.Tables[0].Rows[i]["datetime"].ToString();
                        DtAppLog.Rows.Add(DtRow);
                    }
                    grdviewItemRequest.DataSource = DtAppLog;
                    grdviewItemRequest.DataBind();
                }
                DsAppLog.Tables[0].Clear();
            }
            DtAppLog.Rows.Clear();
            #endregion
            //For the General File
            #region General File
            foreach (string file in LoadGeneralfileName)
            {
                string[] SplitData = file.Split('_');
                string[] Message;
                DsAppLog.ReadXml(file);//Read the XML file and fill it into the Datatable.
                if (DsAppLog.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < DsAppLog.Tables[0].Rows.Count; i++)
                    {
                        DataRow DtRow = DtAppLog.NewRow();
                        DtRow["userid"] = DsAppLog.Tables[0].Rows[i]["userid"].ToString();
                        DtRow["logtype"] = DsAppLog.Tables[0].Rows[i]["logtype"].ToString();
                        Message = DsAppLog.Tables[0].Rows[i]["logmesg"].ToString().Split(',');
                        for (int k = 0; k < Message.Length; k++)
                        {
                            DtRow["logmesg"] += Message[k] + "<br/>";
                        }
                        DtRow["pagename"] = DsAppLog.Tables[0].Rows[i]["pagename"].ToString();
                        DtRow["ipaddr"] = DsAppLog.Tables[0].Rows[i]["ipaddr"].ToString();
                        DtRow["datetime"] = DsAppLog.Tables[0].Rows[i]["datetime"].ToString();
                        DtAppLog.Rows.Add(DtRow);
                    }
                    grdviewGeneral.DataSource = DtAppLog;
                    grdviewGeneral.DataBind();
                }
                DsAppLog.Tables[0].Clear();
            }
            #endregion
        }

        catch (Exception ex)
        {

        }
    }
    //protected void GetData()
    //{
    //    string Path = Server.MapPath("~/XML/");
    //    string[] fileName = Directory.GetFiles(Path, "Log_PurchaseOrder_*.xml", System.IO.SearchOption.TopDirectoryOnly);
    //    if (fileName.Length > 0)
    //    {
    //        foreach (string file in fileName)
    //        {
    //            DataTable DtLog = new DataTable();
    //            DtLog.Columns.Add("userid", typeof(string));
    //            DtLog.Columns.Add("logtype", typeof(string));
    //            DtLog.Columns.Add("logmesg", typeof(string));
    //            DtLog.Columns.Add("pagename", typeof(string));
    //            DtLog.Columns.Add("ipaddr", typeof(string));
    //            DtLog.Columns.Add("datetime", typeof(string));
    //            DsPurchaseOrder.ReadXml(file);
    //            string[] Message;
    //            if (DsPurchaseOrder.Tables[0].Rows.Count > 0)
    //            {
    //                for (int i = 0; i < DsPurchaseOrder.Tables[0].Rows.Count; i++)
    //                {
    //                    DataRow DtRow = DtLog.NewRow();
    //                    DtRow["userid"] = DsPurchaseOrder.Tables[0].Rows[i]["userid"].ToString();
    //                    DtRow["logtype"] = DsPurchaseOrder.Tables[0].Rows[i]["logtype"].ToString();
    //                    Message = DsPurchaseOrder.Tables[0].Rows[i]["logmesg"].ToString().Split(',');
    //                    for (int k = 0; k < Message.Length; k++)
    //                    {
    //                        DtRow["logmesg"] += Message[k] + "<br/>";
    //                    }
    //                    DtRow["pagename"] = DsPurchaseOrder.Tables[0].Rows[i]["pagename"].ToString();
    //                    DtRow["ipaddr"] = DsPurchaseOrder.Tables[0].Rows[i]["ipaddr"].ToString();
    //                    DtRow["datetime"] = DsPurchaseOrder.Tables[0].Rows[i]["datetime"].ToString();
    //                    DtLog.Rows.Add(DtRow);
    //                    //grdview.DataSource = DtLog;
    //                    //grdview.DataBind();                        
    //                }
    //            }
    //        }
    //    }
    //}
    //protected void btnSearch_Click(object sender, EventArgs e)
    //{
    //    string Filter = ""; //GetData();
    //    if (ddlColumn.SelectedValue == "1")//User Name
    //    {
    //        Filter = string.Format(CultureInfo.InvariantCulture, "userid like'%" + txtfilterData.Text + "%'");
    //    }
    //    else if (ddlColumn.SelectedValue == "2")//Log Type
    //    {
    //        Filter = string.Format(CultureInfo.InvariantCulture, "logtype like'%" + txtfilterData.Text + "%'");
    //    }
    //    else if (ddlColumn.SelectedValue == "3")//Page name
    //    {
    //        Filter = string.Format(CultureInfo.InvariantCulture, "pagename like'%" + txtfilterData.Text + "%'");
    //    }
    //    else if (ddlColumn.SelectedValue == "4")//IpAddress
    //    {
    //        Filter = string.Format(CultureInfo.InvariantCulture, "ipaddr like'%" + txtfilterData.Text + "%'");
    //    }
    //    else if (ddlColumn.SelectedValue == "5")//Date
    //    {
    //        Filter = string.Format(CultureInfo.InvariantCulture, "datetime like'%" + txtfilterData.Text + "%'");
    //    }
    //    else
    //    {
    //        //GetData();
    //    }
    //    //DataRow[] Dr = DtLog.Select(Filter);
    //    //DataTable Tempdt = new DataTable();
    //    //Tempdt = DtLog.Clone();
    //    //foreach (DataRow drNew in Dr)
    //    //{
    //    //    Tempdt.ImportRow(drNew);
    //    //}
    //    //grdview.DataSource = Tempdt;
    //    //grdview.DataBind();
    //}
}